package com.sun.project.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.query.MPJQueryWrapper;
import com.sun.common.authoriza.util.CurrentUserUtils;
import com.sun.common.result.ActionResult;
import com.sun.common.result.SupportResult;
import com.sun.common.util.SequenceGenerator;
import com.sun.project.model.ProductModel;
import com.sun.project.model.ProjectProductModel;
import com.sun.project.service.ProductService;
import com.sun.project.service.ProjectProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


//产品操作

@RestController
@RequestMapping("/product")
public class ProductController {

    private static SequenceGenerator sequenceGenerator = new SequenceGenerator();

    @Autowired
    private ProductService productService;
    @Autowired
    private ProjectProductService projectProductService;

    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public SupportResult list(@RequestBody ProductModel productModel) {
        int currentPage = productModel.getCurrentPage()==null?1:productModel.getCurrentPage();
        int pageSize = productModel.getPageSize()==null?10:productModel.getPageSize();
        MPJQueryWrapper<ProductModel> wrapperM=new MPJQueryWrapper<ProductModel>()
                .selectAll(ProductModel.class)
                .leftJoin("zt_projectproduct t1 on t.id=t1.product")
                .leftJoin("zt_project t2 on t2.id=t1.project");
        String status = productModel.getStatus();
        if(StrUtil.isNotBlank(status)) {
            wrapperM.eq("t.status",status);
        }
        String parent = productModel.getParent();
        if(StrUtil.isNotBlank(parent)) {
            wrapperM.eq("t1.project",parent);
        }
        wrapperM.eq("t2.parent","0");
        //调用方法实现分页查询
        IPage<ProductModel> productModelPage = productService.selectJoinListPage(
                new Page<ProductModel>(currentPage,pageSize),ProductModel.class,wrapperM);

        SupportResult rs=new SupportResult(productModelPage);

        rs.setTotalResult((int)productModelPage.getTotal());

        return rs;
    }



    @RequestMapping(value = "/getById", method = RequestMethod.POST)
    public SupportResult getById(@RequestBody ProductModel productModelParam) {
        MPJQueryWrapper<ProductModel> wrapper=new MPJQueryWrapper<ProductModel>()
                .selectAll(ProductModel.class)
                .select("t1.project as parent")
                .leftJoin("zt_projectproduct t1 on t1.product=t.id")
                .leftJoin("zt_project t2 on t1.project=t2.id")
                .eq("t.id",productModelParam.getId())
                .eq("t2.parent","0");

        ProductModel   productModel=  productService.selectJoinOne(ProductModel.class,wrapper);
        SupportResult rs=new SupportResult(productModel);
        return rs;
    }


    @RequestMapping(value = "/saveOrUpdate", method = RequestMethod.POST)
    public  ActionResult saveOrUpdate(@RequestBody ProductModel productModelParam) {
        if(StrUtil.isNotBlank(productModelParam.getId())){
            productService.updateById(productModelParam);
        }else{
            String id=sequenceGenerator.nextStringId();
            productModelParam.setId(id);
            productModelParam.setCreatedBy(CurrentUserUtils.getOperateUserName());
            productModelParam.setCreatedDate(new Date());
            productService.save(productModelParam);

            String parent=productModelParam.getParent();
            ProjectProductModel ppm=new ProjectProductModel();
            ppm.setProject(parent);
            ppm.setProduct(id);
            projectProductService.save(ppm);
        }
        return ActionResult.success();
    }



    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public ActionResult delete(@RequestBody ProductModel productModelParam) {
        productService.removeById(productModelParam.getId());
        Map product=new HashMap<>();
        product.put("product",productModelParam.getId());
        projectProductService.removeByMap(product);
        return ActionResult.success();
    }




    @RequestMapping(value = "/productSelect", method = RequestMethod.POST)
    public SupportResult setSelect(@RequestBody ProductModel productModelParam) {
        MPJQueryWrapper<ProductModel> wrapper=new MPJQueryWrapper<ProductModel>()
                .selectAll(ProductModel.class)
                .leftJoin("zt_projectproduct t1 on t.id=t1.product")
                .eq("t1.project",productModelParam.getParent());

        List<ProductModel> list = productService.selectJoinList(ProductModel.class,wrapper);
        SupportResult rs=new SupportResult(list);
        return rs;
    }
}
